package com.appspot.bibtable.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.bibtable.PMF;
import com.appspot.bibtable.model.ReferenceEntity;
import com.appspot.bibtable.model.UserInfo;
import com.appspot.bibtable.view.TableCellType;
import com.appspot.bibtable.view.html.HtmlTemplate;
import com.appspot.bibtable.view.html.ReferenceReadOnlyTable;
import com.appspot.bibtable.view.html.ReferenceTable;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.UserServiceFactory;

public class DetailItemServlet extends PathQueryHttpServlet
{
	private static final Logger logger = Logger.getLogger(DetailItemServlet.class.getName());

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		Key key = getKey(request);
		if (key == null)
		{
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}

		response.setCharacterEncoding("UTF-8");
		try
		{
			PersistenceManager manager = PMF.get().getPersistenceManager();
			try
			{
				UserInfo userInfo = UserInfo.getUserInfoOf(UserServiceFactory.getUserService().getCurrentUser(), manager);
				ReferenceEntity entity = manager.getObjectById(ReferenceEntity.class, key);

				HtmlTemplate template = new HtmlTemplate();
				ReferenceTable table = new ReferenceReadOnlyTable();
				table.setId("reference-info");
				table.setPersistenceManager(manager);

				template.setTitle(entity.getTitle());
				template.setRequest(request);		
				PrintWriter writer = response.getWriter();
				template.startDocument(writer);
				template.startContent(writer);
				template.printSearchBox(writer);
				table.startTable(writer);
				if (userInfo != null)
				{
					table.startRow(writer);
					table.startCell(writer, TableCellType.HEADING);
					writer.write("Bookmark");
					table.endCell(writer, TableCellType.HEADING);
					table.startCell(writer, TableCellType.NORMAL);
					if (userInfo.isBookmarking(key))
					{
						writer.write("<a href=\"/unbookmark/" + KeyFactory.keyToString(key) + "\" class=\"bookmark\">");
						writer.write("<img src=\"/themes/default/icons/bookmark.png\" width=\"16\" height=\"16\" alt=\"bookmarked\" title=\"Click to unbookmark\" />");
						writer.write("</a>");
					}
					else
					{
						writer.write("<a href=\"/bookmark/" + KeyFactory.keyToString(key) + "\" class=\"bookmark\">");
						writer.write("<img src=\"/themes/default/icons/unbookmark.png\" width=\"16\" height=\"16\" alt=\"not bookmarked\" title=\"Click to bookmark\" />");
						writer.write("</a>");
					}
					table.endCell(writer, TableCellType.NORMAL);
					table.endRow(writer);
				}
				table.addReference(entity, writer);
				table.endTable(writer);
				if (UserInfo.getUserInfoOf(UserServiceFactory.getUserService().getCurrentUser(), manager) != null)
				{
					writer.write("<div id=\"action-link\"><a href=\"/edit/" + KeyFactory.keyToString(key) + "\">edit this item</a></div>");
				}
				template.endContent(writer);
				template.endDocument(writer);
				return;
			}
			catch (JDOObjectNotFoundException ex)
			{
				logger.info(ex.getMessage());
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
			}
			finally
			{
				manager.close();
			}
		}
		catch (IllegalArgumentException ex)
		{
			logger.info(ex.getMessage());
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}
}
